Key to CSE142 Midterm, Autumn 2012 1. Expression Value ----------------------------------------------- 5 + 2 * 6 - 10 * 2 -3 7 % (5 % 3) + 21 % 4 2 2 * 7 + 2 + "*" + 2 + 3 "16*23" !(2 < 4 && 5 > 7) && 16 % 4 == 1 false 107 / 20 * 2 / 3 / 2.0 + 3 / 4.0 2.25 2. Parameter Mystery. The program produces the following output. steph wants to play on the sun play wants to steph on the swim julie wants to meplay on the play me wants to sun on the sun 3. Method Call Output Produced --------------------------------------- ifElseMystery(3, 1); 13 1 2 ifElseMystery(2, 6); 12 6 3 ifElseMystery(5, -1); 5 9 5 ifElseMystery(1, 2); 1 12 4 4. Method Call Output Produced --------------------------------------- mystery(5, 4); 4, 9 mystery(6, 8); 8, 14, 21 mystery(3, 6); 6, 9, 13 mystery(1, 5); 5, 6, 8, 11 5. x < 5 x == 0 z > 0 +---------------------+---------------------+---------------------+ Point A | sometimes | sometimes | never | +---------------------+---------------------+---------------------+ Point B | sometimes | never | sometimes | +---------------------+---------------------+---------------------+ Point C | always | never | always | +---------------------+---------------------+---------------------+ Point D | sometimes | sometimes | sometimes | +---------------------+---------------------+---------------------+ Point E | always | always | sometimes | +---------------------+---------------------+---------------------+ 6. Three possible solutions appear below: public static int minOfThree(int a, int b, int c) { if (a < b && a < c) { return 1; } else if (b < a && b < c) { return 2; } else if (c < a && c < b) { return 3; } else { return -1; } } public static int minOfThree(int a, int b, int c) { if ((a == b && c >= a) || (a == c && b >= c) || (b == c && a >= b)) { return -1; } else { int p = 1; int s = a; if (b < s) { p = 2; s = b; } if (c < s) { p = 3; s = c; } return p; } } public static int minOfThree(int x, int y, int z) { boolean repeat = false; int minPos = 1; int minNum = x; if (y < minNum) { minNum = y; minPos = 2; } else if (y == minNum) { repeat = true; } if (z < minNum) { minNum = 2; minPos = 3; repeat = false; } else if (z == minNum) { repeat = true; } if (repeat == true) { return -1; } else { return minPos; } } 7. One possible solution appears below: public static void evens(Scanner console, int target) { int even = 0; int count = 0; while (even < target) { System.out.print("number? "); int val = console.nextInt(); count++; if (val % 2 == 0) { even++; } else { even = 0; } } System.out.println(target + " even in a row after " + count + " tries."); } 8. Three possible solutions appears below: public static int countWords(String s) { int count = 0; boolean inWord = false; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == ' ') { inWord = false; } else if (!inWord) { count++; inWord = true; } return count; } } public static int countWords(String s) { int count = 0; for (int i = 1; i < s.length(); i++) { if (s.charAt(i - 1) == ' ' && s.charAt(i) != ' ') { count++; } } if (s.length() > 0 && s.charAt(0) != ' ') { count++; } return count; } public static int countWords(String s) { int count = 0; int i = 0; while (i < s.length()) { while (i < s.length() && s.charAt(i) == ' ') { i++; } if (i < s.length()) { count++; while (i < s.length() && s.charAt(i) != ' ') { i++; } } } return count; }